package com.leapp.sk.viewdelegate.im

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import com.example.baselibrary.util.SKPermissionUtils
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.imgselector.ImageSelector
import com.leapp.sk.R
import com.leapp.sk.bean.EmojiconEntity
import com.example.im.SKIMSend
import com.leapp.sk.ui.activity.im.SingleChatActivity
import com.example.util.im.MessagePresenter
import com.leapp.sk.view.im.SKChatInputMenu
import com.leapp.sk.view.im.SKChatMessageList
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.video.VideoActivity
import com.example.util.BCFinalist
import java.io.File


/**
 * 单聊视图托管类
 * @author  chaohaipeng  on 2018/7/13.
 */
class SingleChatDelegale : BaseAppDelegate(), SKChatInputMenu.ChatInputMenuListener {

    /**
     * 会话Id
     */
    var conversationId: String? = null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_single_chat
    }

    /**
     * 初始化页面布局
     */
    fun initView(id: String) {
        this.conversationId = id
        val sim_asc_input_menu = getView<SKChatInputMenu>(R.id.sim_asc_input_menu)
        sim_asc_input_menu.setChatInputMenuListener(this)
        sim_asc_input_menu.init(getActivity<AppCompatActivity>())
        initListData(id)
    }

    /**
     * 判断菜单是否打开
     */
    fun meunIsOpen(): Boolean {
        val sim_asc_input_menu = getView<SKChatInputMenu>(R.id.sim_asc_input_menu)
        if (sim_asc_input_menu.meunIsOpen()) {
            return true
        }
        return false
    }

    /**
     * 设置标题
     */
    fun initTobBar(title: String?) {
        val topBar = getView<SKTopBar>(R.id.jtb_asc)
        if (!TextUtils.isEmpty(title)) {
            topBar?.setTitle(title)
        }
        topBar.setEventInterface(object : SKTopBar.EventInterface {
            override fun leftOnClick() {
                getActivity<SingleChatActivity>().finish()
            }
            override fun rightOnClick() {
            }
        })

    }


    /**
     * 照片选择
     */
    private fun pickImage() {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(9)
                .setGridColumns(3)
                .setShowCamera(true)
                .startSelect(getActivity<SingleChatActivity>())
    }

    /**
     * 初始化页面数据
     */
    fun initListData(id: String) {
        val sml_asc_list = getView<SKChatMessageList>(R.id.sml_asc_list)
        sml_asc_list.init(getActivity(), id)
        sml_asc_list.loadMessage(id, "", 20)
    }


    /**
     * 消息更新监听初始化
     */
    fun initMegListener(messagePresenter: MessagePresenter?) {
        if (messagePresenter != null) {
            val sml_asc_list = getView<SKChatMessageList>(R.id.sml_asc_list)
            sml_asc_list.initMegListener(messagePresenter)
        }
    }


    override fun onSendMessage(content: String) {
        if (conversationId != null) {
            SKIMSend.getInstance().sendTxt(conversationId!!, content, LKIMChatType.SingleChat)
        }
    }

    override fun onBigExpressionClicked(emojicon: EmojiconEntity) {
        if (conversationId != null) {
            SKIMSend.getInstance().sendTxt(conversationId!!, emojicon.identityCode + "", LKIMChatType.SingleChat)
        }
    }

    override fun onImageExpressionClicked() {
        pickImage()
    }

    override fun onVideoExpressionClicked() {
        val requestAudio = SKPermissionUtils.getInstance().requestAudio(getActivity())
        val requestCamera = SKPermissionUtils.getInstance().requestCamera(getActivity())
        if (requestAudio&&requestCamera){
            val intent: Intent = Intent(getActivity<SingleChatActivity>(), VideoActivity::class.java)
            getActivity<SingleChatActivity>().startActivityForResult(intent, BCFinalist.VIDEO_PLAY_REQUEST)
        }

    }

    override fun onSendVoice(voiceDuration: Int, voicePath: String, voiceFileName: String, voiceFile: File) {
        if (conversationId != null) {
            SKIMSend.getInstance().sendVoice(conversationId!!, voiceFile.absolutePath, voiceDuration, LKIMChatType.SingleChat)
        }

    }
}